package com.gome.ocean.service.authority.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.gome.ocean.dao.authority.RoleFunctionDAO;
import com.gome.ocean.dao.model.authority.RoleFunction;
import com.gome.ocean.service.authority.RoleFunctionService;
import com.google.common.collect.Lists;

/**
 * Created by liuqingxia on 2016/6/1.
 */
@Service
public class RoleFunctionServiceImpl implements RoleFunctionService {
    
    private static final Logger LOG = LoggerFactory.getLogger(RoleFunctionServiceImpl.class);

    @Autowired
    private RoleFunctionDAO roleFunctionDAO;

    @Override
    public boolean insertRoleFunction(RoleFunction roleFunction) {
        return roleFunctionDAO.insertRoleFunction(roleFunction) == 1;
    }

    @Override
    public void insertRoleFunction(Long roleId, List<Long> funcIds) {
    	List<RoleFunction> list = Lists.newArrayList();
        for (Long funcId : funcIds) {
            RoleFunction roleFunction = new RoleFunction();
            roleFunction.setRoleid(roleId);
            roleFunction.setFuncid(funcId);
            roleFunction.setCreateTime(new Date());
            list.add(roleFunction);
        }
        try {
        	roleFunctionDAO.insertRoleFuncList(list);
		} catch (Exception e) {
		    LOG.error("异常信息" + e.toString());
		}
        
    }

    @Override
    public boolean removeRoleFunction(Long id) {
        return roleFunctionDAO.removeRoleFunctionById(id) == 1;
    }

    @Override
    public boolean updateRoleFunction(RoleFunction roleFunction) {
        return roleFunctionDAO.updateRoleFunction(roleFunction) == 1;
    }

    @Override
    public RoleFunction findRoleFunctionById(Long id) {
        return roleFunctionDAO.findRoleFunctionById(id);
    }

    @Override
    public Page<RoleFunction> findAllRoleFunctions(int pageNum, int pageSize) {
        return (Page<RoleFunction>) roleFunctionDAO.findAllRoleFunctions(new RowBounds(pageNum, pageSize));
    }

    @Override
    public List<RoleFunction> findRoleFunctionByRoleId(Long id) {
        return roleFunctionDAO.findRoleFunctionByRoleId(id);
    }

	@Override
	public boolean deleteRoleFuncByRoleId(Long roleId) {
		boolean flag = false;
		try {
			Integer i = roleFunctionDAO.deleteRoleFuncByRoleId(roleId);
			if (i>=0) {
				flag = true;
			}
		} catch (Exception e) {
		    LOG.error("异常信息" + e.toString());
		}
		return flag;
	}
}
